<template>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" :width="dialogInfo.width" align-center>
    <el-form ref="formRef" label-position="left" :model="formModel" :rules="formRules" label-width="120px">
      <el-row>
        <el-col :sm="24">
          <el-form-item label="课程名称" prop="name">
            <el-input clearable class="w100" v-model.trim="formModel.name" autocomplete="off" placeholder="请输入教室名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="所属专业" prop="academyId">
            <el-select clearable class="w200px mr-3" v-model="formModel.academyId" placeholder="请选择学院" autocomplete="off" @change="getMajorList">
              <el-option v-for="item in academyOption" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <el-select clearable class="w200px" v-model="formModel.majorId" placeholder="请选择专业" autocomplete="off" :disabled="!formModel.academyId">
              <el-option v-for="item in majorOption" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

<!--        <el-col :sm="24">-->
<!--          <el-form-item label="访问权限" prop="degreeType">-->
<!--            <el-radio-group v-model="formModel.degreeType">-->
<!--              <el-radio v-for="item in degreeTypeOption" :label="item.key" :key="item.key">{{item.label}}</el-radio>-->
<!--            </el-radio-group>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :sm="24">
          <el-form-item label="课程分类" prop="degreeType">
            <el-radio-group v-model="formModel.degreeType">
              <el-radio v-for="item in classOption" :label="Number(item.key)" :key="Number(item.key)">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="课程性质" prop="courseType">
            <el-radio-group v-model="formModel.courseType">
              <el-radio v-for="item in courseTypeOption" :label="item.courseTypeName" :key="item.id" >{{item.courseTypeName}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="课程简介" prop="remark">
            <el-input v-model="formModel.remark" :rows="2" type="textarea" placeholder="请输入" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info" plain @click="onCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 管理员编辑组件
 */
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import { getNewObj } from '@/utils/EsCommon';
import { courseParams, degreeTypeList, courseTypeList, classList } from '@/apis/model/CourseModel';




// ======================================================
//                      变量声明
// ======================================================

// emit
const emit = defineEmits(['success']);
// 表单数据
const formAttrs = getNewObj(courseParams());

const formModel = ref(Object.assign({}, formAttrs));
// 弹窗信息
const dialogInfo = ref({
  visible: false,
  title: '',
  width: '600px',
})
// 表单组件的引用实例
const formRef = ref(null);

// 选项数据
const degreeTypeOption = ref(degreeTypeList);
const courseTypeOption = ref([]);
const classOption = ref(classList);

const academyOption = ref([]);
const majorOption = ref([]);
// 表单校验规则
const formRules = ref({
  name: [{ required: true, trigger: "blur", message: "请输入课程名称" }],
  academyId: [{ required: true, trigger: "blur", message: "请输入所属专业" }],
  degreeType: [{ required: true, trigger: "blur", message: "请输入课程分类" }],
  courseType: [{ required: true, trigger: "blur", message: "请输入课程性质" }],
  remark: [{ required: true, trigger: "blur", message: "请输入课程简介" }],
})

// ======================================================
//                      点击操作
// ======================================================

// 点击提交按钮
const onSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      console.log('表单校验失败：', fields);
      return;
    }
    let url = formModel.value.id > 0 ? ApiUrls.courseChange : ApiUrls.courseAdd;
    EsAjax.post(url, formModel.value).then(res => {
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      dialogInfo.value.visible = false;
      ElMessage.success('提交成功');
      emit('success');
    }).catch(err => {
      ElMessage.error('网络错误：' + err.message);
    })
  })
}

// 点击取消按钮
const onCancel = () => {
  formModel.value = Object.assign({}, formAttrs);
  formRef.value.resetFields();
  dialogInfo.value.visible = false;
}
// ======================================================
//                      处理事件
// ======================================================

// 加载数据
const show = async (id, title, item = null, width = '600px') => {
  dialogInfo.value.title = title;
  dialogInfo.value.width = width;
  dialogInfo.value.visible = true;
  formModel.value = Object.assign({}, formAttrs)
  // 加载详情数据
  if (id) {
    const _form = getNewObj(item);
    formModel.value = {
      id:id,
      "name":_form.name,
      "academyId":_form.Academy.id,
      "majorId":_form.Major.id,
      "degreeType":_form.degreeType,
      "courseType":_form.courseType,
      "remark":_form.remark,
      // "TeacherId":_form.Teacher.id,
      // "semesterId":1,
      // "roomId":1,
      // "classWeek":"1,2,3,4,5",
      // "weekday":1,
      // "classId":2
    };
    if(formModel.value.academyId){
      getMajorList(formModel.value.academyId)
    }
  }
  getAcademyList();
  getCourseTypeList()
}

const getAcademyList = () => {
  EsAjax.post(ApiUrls.academyList, { params: {} }).then(res => {
    console.log(res)

    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    academyOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}
const getCourseTypeList = () => {
  EsAjax.post(ApiUrls.courseTypeList, { }).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    courseTypeOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

const getMajorList = (academyId) => {
  majorOption.value = []
  if(academyId){
    EsAjax.post(ApiUrls.majorList, { academyId }).then(res => {
      console.log(res)
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      let { list } = res.data.data
      majorOption.value = list;
    }).catch(err => {
      ElMessage.error(err.message);
    })
  }
}


defineExpose({
  show
})
</script>

<style scoped>
</style>
